import keymaster from 'keymaster'

// 公用方法
// 列表公用热键
export const publicListKey = {
  mounted() {
    keymaster('f9', this.f9)
    keymaster('enter', this.toQuery)
  },
  activated() {
    keymaster('f9', this.f9)
    keymaster('enter', this.toQuery)
  },
  methods: {
    // f9
    f9() {
      this.crud.props.searchToggle = !this.crud.props.searchToggle
    },
    toQuery() { this.crud.toQuery() }
  },
  // 解除
  deactivated() {
    keymaster.unbind('enter')
    keymaster.unbind('f9')
  },
  beforeDestroy() {
    keymaster.unbind('enter')
    keymaster.unbind('f9')
  }
}
// 详情公用热键
export const publicKey = {
  mounted() {
    keymaster('f11', this.f9)
    keymaster('enter', this.toQuery)
  },
  activated() {
    keymaster('f11', this.f9)
    keymaster('enter', this.toQuery)
  },
  methods: {
    // f9
    f9() {
      this.crud.props.searchToggle = !this.crud.props.searchToggle
    },
    toQuery() { this.crud.toQuery() }
  },
  // 解除
  deactivated() {
    keymaster.unbind('f9,enter')
  },
  beforeDestroy() {
    keymaster.unbind('f9,enter')
  }
}

// 销售单热键
export const salesDetailKey = {
  data() {
    return {
      columnIndex: 0,
      showQueryGoods: false
    }
  },
  mounted() {
    window.addEventListener('keydown', this.keyDown)
  },
  activated() {
    window.addEventListener('keydown', this.keyDown)
  },
  deactivated() {
    window.removeEventListener('keydown', this.keyDown)
  },
  beforeDestroy() {
    window.removeEventListener('keydown', this.keyDown)
  },
  methods: {
    keyDown() {
      try {
        if (this.showAddBatchDialog || this.showQueryGoods) {
          return
        }
        if (event.keyCode === 121) {
          if (this.crud.order.orderStatus !== 'PASS') {
            event.preventDefault()
            this.toAddList()
          }
        }
        const elTable = this.$refs['table'].$el
        const row = this.$refs.table.store.states.currentRow
        let index, cells
        const list = []
        if (row !== null) {
          index = row.index
          cells = elTable.querySelectorAll('.el-table__body tr')[index].querySelectorAll('.el-input__inner,.el-textarea__inner')
          cells.forEach(item => {
            if (item.placeholder != '请输入内容搜索') {
              list.push(item)
            }
          })
        } else {
          index = -1
          cells = elTable.querySelectorAll('.el-table__body tr')[0].querySelectorAll('.el-input__inner,.el-textarea__inner')
        }

        if (list[this.columnIndex] !== document.activeElement) {
          for (let i = 0; i < list.length; i++) {
            if (list[i] === document.activeElement) {
              this.columnIndex = i
              break
            }
          }
        }
        if (this.crud.order.orderStatus !== 'PASS') {
          if (event.keyCode === 39) {
            if (this.columnIndex < list.length - 1) {
              this.columnIndex++
            }
            list[this.columnIndex].focus()
            setTimeout(() => { list[this.columnIndex].select() }, 10)
          } else if (event.keyCode === 40) {
            if (this.crud.data[index + 1] === undefined) {
              new Promise((resolve, reject) => {
                this.toAdd()
                this.$refs.table.setCurrentRow(this.crud.data[index + 1])
                this.$forceUpdate()
                resolve('yes')
              }).then(() => {
                this.rowIndex = index + 1
                setTimeout(() => {
                  cells = elTable.querySelectorAll('.el-table__body tr')[this.rowIndex].querySelectorAll('.el-input__inner,.el-textarea__inner')
                  const list = []
                  cells.forEach(item => {
                    if (item.placeholder != '请输入内容搜索') {
                      list.push(item)
                    }
                  })
                  list[0].focus()
                  setTimeout(() => { list[0].select() }, 10)
                  this.columnIndex = 0
                }, 200)
              })
            } else {
              this.rowIndex = index + 1
              this.$refs.table.setCurrentRow(this.crud.data[this.rowIndex])
              setTimeout(() => {
                cells = elTable.querySelectorAll('.el-table__body tr')[this.rowIndex].querySelectorAll('.el-input__inner,.el-textarea__inner')
                const list = []
                cells.forEach(item => {
                  if (item.placeholder != '请输入内容搜索') {
                    list.push(item)
                  }
                })
                list[this.columnIndex].focus()
                setTimeout(() => { list[this.columnIndex].select() }, 10)
              }, 200)
            }
          } else if (event.keyCode === 38) {
            if (index > 0) {
              this.rowIndex = index - 1
              this.$refs.table.setCurrentRow(this.crud.data[this.rowIndex])
              setTimeout(() => {
                cells = elTable.querySelectorAll('.el-table__body tr')[this.rowIndex].querySelectorAll('.el-input__inner,.el-textarea__inner')
                const list = []
                cells.forEach(item => {
                  if (item.placeholder != '请输入内容搜索') {
                    list.push(item)
                  }
                })

                list[this.columnIndex].focus()
                setTimeout(() => { list[this.columnIndex].select() }, 10)
              }, 200)
            }
          } else if (event.keyCode === 37) {
            if (this.columnIndex > 0) {
              this.columnIndex--
            }
            list[this.columnIndex].focus()
            setTimeout(() => { list[this.columnIndex].select() }, 10)
          } else if (event.keyCode === 13) {
            if (list[this.columnIndex] == document.activeElement) {
              if (list[this.columnIndex].placeholder === '请输入商品') {
                this.queryGoods = { keyword: list[this.columnIndex].value, index: this.rowIndex, ignoreGoodsTagNames: '回瓶回箱,瓶盖',goodsTagName: this.queryGoods.goodsTagName||''}
                this.showQueryGoods = true
              } else {
                list[this.columnIndex].blur()
              }
            } else {
              list[this.columnIndex].focus()
            }
          }
        }

        if (event.keyCode === 122) {
          event.preventDefault()
          event.returnValue = !event.returnValue
          this.showPriceDetail(row)
        }
      } catch (e) {

      }
    } }
}
// 选择商品热键
export const selectGoods = {
  data() {
    return {
      columnIndex: 0
    }
  },
  mounted() {
    window.addEventListener('keydown', this.keyDown)
  },
  activated() {
    window.addEventListener('keydown', this.keyDown)
  },
  deactivated() {
    window.removeEventListener('keydown', this.keyDown)
  },
  beforeDestroy() {
    window.removeEventListener('keydown', this.keyDown)
  },
  methods: {
    keyDown() {
      if (!this.$parent.$parent.showAddBatchDialog) {
        return
      }
      let index
      try {
        var row = this.$refs.leftTable.store.states.currentRow
        if (row.goodsId !== undefined) {
          index = getIndex(this.goodsList, row.goodsId)
        } else {
          index = -1
        }
      } catch (e) {
        index = -1
      }
      if (event.keyCode === 40) {
        this.$refs.leftTable.setCurrentRow(this.goodsList[index + 1])
      } else if (event.keyCode === 38) {
        if (index > 0) {
          this.$refs.leftTable.setCurrentRow(this.goodsList[index - 1])
        }
      } else if (event.keyCode === 32) {
        event.preventDefault()
        if (getIndex(this.resultGoods, row.goodsId) === -1) {
          this.resultGoods.push(row)
          this.$forceUpdate()
        }
        setTimeout(this.$refs.leftTable.setCurrentRow(row), 200)
      } else if (event.keyCode === 13) {
        event.preventDefault()
        this.submitCU()
      }
    }
  }
}

function getIndex(list, value) {
  for (let i = 0; i < list.length; i++) {
    if (list[i].goodsId === value) {
      return i
    }
  }
  return -1
}

// 单选商品热键
export const queryGoods = {
  data() {
    return {
      columnIndex: 0
    }
  },

  mounted() {
    window.addEventListener('keydown', this.keyDown)
  },
  activated() {
    window.addEventListener('keydown', this.keyDown)
  },
  deactivated() {
    window.removeEventListener('keydown', this.keyDown)
  },
  beforeDestroy() {
    window.removeEventListener('keydown', this.keyDown)
  },
  methods: {
    getElementTop(element) {
      var actualTop = element.offsetTop
      var current = element.offsetParent
      while (current !== null) {
        actualTop += current.offsetTop
        current = current.offsetParent
      }
      return actualTop
    },

    keyDown() {
      if (!this.$parent.$parent.showQueryGoods) {
        return
      }
      let index
      try {
        var row = this.$refs.table.store.states.currentRow
        if (row.goodsId !== undefined) {
          index = getIndex(this.goodsList, row.goodsId)
        } else {
          index = -1
        }
      } catch (e) {
        index = -1
      }
      if (event.keyCode === 40) {
        this.$refs.table.setCurrentRow(this.goodsList[index + 1])
        var dom = this.$refs.table.bodyWrapper
        this.$nextTick(() => {
          dom.scrollTo(0, (index + 1) * 29)
        })
        this.$refs.ipt.blur()
      } else if (event.keyCode === 38) {
        if (index > 0) {
          this.$refs.table.setCurrentRow(this.goodsList[index - 1])
          var dom = this.$refs.table.bodyWrapper
          this.$nextTick(() => {
            dom.scrollTo(0, (index - 1) * 29)
          })
          this.$refs.ipt.blur()
        } else {
          var dom = this.$refs.table.bodyWrapper
          this.$nextTick(() => {
            dom.scrollTo(0, index * 29)
          })
          this.$refs.table.setCurrentRow(null)
          this.$refs.ipt.focus()
        }
      } else if (event.keyCode === 13) {
        if (this.$refs.ipt == document.activeElement) {
          this.queryGoods01()
          this.$refs.ipt.blur()
        } else {
          this.pushGoods()
        }
      }
    }
  }
}

